import pickle
#import Test.py
#游戏类
class Save_load():
    def __init__(self,maxx,maxy):
        self.maxx=maxx
        self.maxy=maxy
        self.who=False
    def Save(self,Checkboard,who):
        fpath=input('保存路径：')
        file=open(fpath,'wb')
        self.who=who
        pickle.dump(self,file)
        file.close()
    def Load(self,):
        fpath=input('打开路径：')
        import os
        if os.access(fpath,os.F_OK):
            file=open(fpath,'rb')
            status=pickle.load(file)
            file.close()
        else:
            print('文件不存在！')
#游戏类
# 判断棋局
def isWon(Checkboard, xPoint, yPoint):
    result1=CheckFiveRow(Checkboard, xPoint, yPoint,1,0)        #y=0方向
    result2 = CheckFiveRow(Checkboard, xPoint, yPoint, 0, 1)    #x=0方向
    result3 = CheckFiveRow(Checkboard, xPoint, yPoint, 1, -1)   #x=-y方向
    result4 = CheckFiveRow(Checkboard, xPoint, yPoint, 1, 1)    #x=y方向
    return result1 or result2 or result3 or result4
# 判断棋局
def inRang(xPoint,yPoint):
    return xPoint<maxx and xPoint>=0 and yPoint<maxy and yPoint>=0
#输赢判断
def CheckFiveRow(Checkboard, xPoint, yPoint,xdir,ydir):
    count=1
    t=Checkboard[xPoint][yPoint]
    x,y=xPoint,yPoint
    while t==Checkboard[x+xdir][y+ydir]:    #以自己为原点正向计数
        count+=1
        x += xdir
        y += ydir
    x,y=xPoint,yPoint   #还原xy，逆向计数
    while t==Checkboard[x-xdir][y-ydir]:    #以自己为原点逆向计数
        count+=1
        x-=xdir
        y-=ydir
    x,y=xPoint,yPoint   #并没有必要还原，只是一个习惯
    if count>4:
        return True
    else:
        return False
#输赢判断
def PrintCheckboard(Checkboard):
    print("                            五子棋益智游戏             ")
    print("       0      1      2      3      4      5      6      7      8      9\n")
    for i in range(maxx):
        print(i,end="")
        for j in range(maxy):
            if Checkboard[i][j]==0:
                print("     空",end="")
            if Checkboard[i][j]==1:
                print("     黑",end="")
            if Checkboard[i][j]==2:
                print("     白",end="")
        print("\n")
#打印棋盘

#打印棋盘
#记录棋局状态
def Play(Checkboard):
    End_chess=False
    who=True#黑方执子
    while not End_chess:
        t=input('请黑方下子,形如x,y：'if who else'请白方下子,形如x,y：').split(',')
        if(len(t)==2):
            x=int(t[0])
            y=int(t[1])
            if not inRang(x,y):
                print("输入不规范！")
                Play(Checkboard)
            if(Checkboard[x][y])==0:
                if who:
                    Checkboard[x][y]=1
                else:
                    Checkboard[x][y]=2
                PrintCheckboard(Checkboard)
                #判断输赢
                Result=isWon(Checkboard,x,y)
                #判断输赢
                if Result:
                    print("黑方胜" if who else "白方胜")
                    End_chess=True
                else:
                    who=not who
            else:
                print("此位置有子")
        elif len(t)==1:
            if t[0]=='S':
                Begin_End.Save(Checkboard,who)
            if t[0]=='L':
                status=Begin_End.Load()
                Checkboard=status.Checkboard
                who=status.who
                PrintCheckboard(Checkboard)
#记录棋局状态
#主程序
if __name__=='__main__':
    maxx=10
    maxy = 10
    Checkboard=[
        [0,0,0,0,0,0,0,0,0,0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]
    Begin_End=Save_load(max,maxy)
    print("10*10五子棋盘初始化成功，祝您游戏愉快")
    print("游戏内输入S保存，输入L加载")
    print("输入坐标数值范围在0-9")
    Play(Checkboard)
#主程序